﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yahee.PDC.Products;

namespace SysHelper
{
    /// <summary>
    /// 1.0 Yahee服务封装
    /// </summary>
    public static   class YaheeService
    {
        #region  1.0 实例Yahee产品服务接口 -client
        /// <summary>
        /// 1.0 实例Yahee产品服务接口
        /// </summary>
        static ProductServiceClient client = new ProductServiceClient(); 
        #endregion

        #region 2.0 获取 YaHee 产品信息 + static List<MODEL.InterfaceProductModel> GetProductInfo(string[] skuArr)
        /// <summary>
        /// 2.0 获取 YaHee 产品信息
        /// </summary>
        /// <param name="skuArr"></param>
        /// <returns></returns>
        public static List<MODEL.InterfaceProductModel> GetProductInfo(string[] skuArr)
        {
            if (skuArr.Length > 1)
            {
                FindProductsRequest request = new FindProductsRequest();
                request.SKUList = skuArr;
                FindProductsResponse response = client.FindProducts(request);
                if (response != null)
                {
                    return Convert(response.Products);
                }
                else
                {
                    return null;
                }
            }
            else
            {
                GetProductRequest request = new GetProductRequest()
                {
                    Sku = skuArr[0]
                };
                GetProductResponse response = client.GetProduct(request);
                if (response != null)
                {
                    return Convert(new Product[1] { response.Product });
                }
                else
                {
                    return null;
                }
            }
        } 
        #endregion
         
        #region 3.0 转换成Amazon 系统(显示Yahee)的数据结构 - static List<MODEL.InterfaceProductModel> Convert(Product[] ProductArr)
        /// <summary>
        /// 3.0 转换成Amazon 系统(显示Yahee)的数据结构
        /// </summary>
        /// <param name="ProductArr"></param>
        /// <returns></returns>
        private static List<MODEL.InterfaceProductModel> Convert(Product[] ProductArr)
        {
            List<MODEL.InterfaceProductModel> list = new List<MODEL.InterfaceProductModel>();
            foreach (var P in ProductArr)
            {
                MODEL.InterfaceProductModel model = new MODEL.InterfaceProductModel();
                model.Sku = P.Id;
                model.SkuAlias = P.SKUAlias;
                model.SaleStatus = P.SaleStatus.ToString();
                model.PictureArr = new List<string>();
                foreach (var p in P.PicturePaths)
                {
                    model.PictureArr.AddRange(p.Paths.ToList());
                }
                list.Add(model);
            }
            return list;
        } 
        #endregion

    }
}
